//
//  MYNewsHotActionSearchVC.m
//  MuYing
//
//  Created by tbb on 2019/8/6.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYNewsHotActionSearchVC.h"
#import "MYShopSearchScreenHeader.h"
#import "MYNewsDetailsHotTopicVC.h"
#import "MYNewsSearchCell.h"
#import "MYNewsHotActionDetailsVC.h"
@interface MYNewsHotActionSearchVC ()
@property (nonatomic, strong) NSMutableDictionary * parameters;
@end

@implementation MYNewsHotActionSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setupView {
    [super setupView];
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYNewsSearchCell" bundle:nil] forCellReuseIdentifier:@"MYNewsSearchCell"];
    
}

-(void)getNewList {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    self.parameters[@"page"] = @(self.page);
    [YQPNetWorkingManager sendPostRequest:kPOST_INFORMATION_ARTCLE_LIST_URL parameters:self.parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            NSArray *arr = [NSArray modelArrayWithClass:MYNewsHotModel.class json:responseObject.data[@"list"]];
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self stopRefresh];
        YQUITIPSERROR(error)
    }];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYNewsHotModel *dynamicModel = nil;
    if (indexPath.row < self.dataArr.count) {
        dynamicModel = self.dataArr[indexPath.row];
    }
    MYNewsSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsSearchCell"];
    cell.titleLb.text = dynamicModel.title;
    cell.timeLb.text = dynamicModel.createTimeStr;
    [cell.newsIcon setImageURL:YQSTRINGTOURL(dynamicModel.informationImage)];
    cell.likeCountLb.text = [NSString stringWithFormat:@"%@ %@",kYQLS(@"浏览量"),dynamicModel.likeNumber];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 97;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MYNewsHotModel *dynamicModel = nil;
    if (indexPath.row < self.dataArr.count) {
        dynamicModel = self.dataArr[indexPath.row];
    }
    MYNewsHotActionDetailsVC *detailsVC = [MYNewsHotActionDetailsVC new];
    detailsVC.newsModel = dynamicModel;
    kYQPUSH(detailsVC);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYShopSearchScreenHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopSearchScreenHeader"];
    self.screenHeaderView = header;
    [header.synthesizeBtn addTarget:self action:@selector(synthesizeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [header.sellBtn addTarget:self action:@selector(sellAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [header.screenBtn addTarget:self action:@selector(screenAction:) forControlEvents:(UIControlEventTouchUpInside)];
    header.sellBtn.hidden = YES;
    return header;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 0.001;
//}

#pragma makr -- YQSearchBaseBarDelegate
- (void)yq_searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.parameters[@"keyword"] = searchBar.text;
    [self onStartRefresh];
}
-(void)yq_textFieldShouldClear:(UITextField *)textField {
    self.parameters[@"keyword"] = @"";
    [self onStartRefresh];
}
-(void)yq_shouldChangeText:(NSString *)text {
    self.parameters[@"keyword"] = text;
    [self onStartRefresh];
}
#pragma makr -- YQSearchBaseBarDelegate end

#pragma mark --lazy
-(NSMutableDictionary *)parameters {
    if (!_parameters) {
        _parameters = [NSMutableDictionary dictionary];
        _parameters[@"informationCategoryId"] = self.categoryId;
        _parameters[@"limit"] = @"10";
        _parameters[@"keyword"] = @"";
    }
    return _parameters;
}
#pragma mark --lazy end
@end
